<?php
namespace Home\Controller;
use Utuo\ForegroundController;

class SiteController extends ForegroundController {
	
	public function _init() {
		$this->checkLogin();
		$this->setModelName();
	}
	
    public function indexAction() {
    	$this->display();
    }
    
    /**
     * 刮刮卡
     */
    public function scratchAction() {
    	$activityId = I("ac", 0, "intval"); // 活动id
    	$activityScratch = $this->getScratch($activityId);
    	$activityScratch and $this->error("活动未找到或已过期"); exit;
		$this->assign("activityScratch", $activityScratch);
		$this->display("Site/".ACTION_NAME);
    }
    
    /**
     * 刮刮卡抽奖
     */
    public function scratchPostAction() {
    	$activityId = I("ac", 0, "intval"); // 活动id
    	$activityScratch = $this->getScratch($activityId);
    	$activityScratch and $this->ajaxError("活动未找到或已过期");
    	$m = M("activityScratchRecord");
	    $scratchRecord = $m->where(array(
	    		'uid' => $this->uid,
	    		'cid' => $this->cid,
	    		'ac_id' => $activityScratch['id'],
	    	))->find();
	    if($scratchRecord) {
	    	date("Ymd") == $scratchRecord['date'] and $scratchRecord['times'] > $activityScratch['day_times'] and $this->ajaxError("今日最多刮".$activityScratch['day_times']."次");
	    	$scratchRecord['prize'] > 0 and $this->ajaxError("今日已中奖，不能在刮了!");
	    }else{
	    	$id = $m->add(array(
	    		'cid' => $this->cid,
		    	'uid' => $this->uid,
		    	'ac_id' => $activityScratch['id'],
		    	'date' => date("Ymd"),
	    	)) and false !== $id or $this->ajaxError("刮奖信息保存失败，请重试");
	    	$scratchRecord = $m->find($id);
	    }
	    /*抽奖过程*/
	    $prizeConfig = $activityScratch['prize_config'];
	    $level = $this->getRand($prizeConfig);
	    $m->where("id=%d", $scratchRecord["id"])->save(array("prize" => $level[0], "times" => $scratchRecord['times']+1, "date"=>date("Ymd")));
	    $level[0] and $this->ajaxSuccess("恭喜您中了".$prizeConfig[$level[1]]['title'].":".$prizeConfig[$level[1]]['prizeName']);
	    $this->ajaxSuccess("未中奖继续努力!");
    }
    
    /**
     * 获得中奖随机信息
     * @param unknown $prizeConfig
     * @return number
     */
    private function getRand($prizeConfig) {
    	//随机获得一个幸运数字
    	$luck_num = mt_rand( 0, 99 );
    	//初始化几率区间
    	$lucky_range = 0;
    	foreach( $prizeConfig as $k => $v ){
    		$ratio = intval( $v['ratio'] );
    		if( $luck_num >= $lucky_range && $luck_num < $lucky_range + $ratio ){
    			$level = $v['level'];
    			$index = $k;
    			break;
    		}else{
    			$lucky_range += $ratio;
    		}
    	}
    	return array($level*1, $index*1);
    }
    
    /**
     * 获得刮刮卡活动
     * @param unknown $activityId
     * @return mixed
     */
    private function getScratch($activityId) {
    	$now = time();
    	$activityScratch = M('activityScratch')->where(array(
    			'id' => $activityId,
    			'cid' => $this->cid,
    			'model_id' => $this->getModelId(),
    			'begin_time' => array("exp", " <=".$now." and end_time >".$now)
    	))->find(); // 获得一个进行中的活动
    	$activityScratch and $activityScratch['prize_config'] = unserialize($activityScratch['prize_config']);
    	return $activityScratch;
    }
    
}